<?php
class RightAction extends Action {
	//初始化操作
	public function _initialize() {
		$role = M ( 'Role' );
		$list = $role->select ();
		$this->assign ( 'list', $list );
		//用户权限检查
		if (C ( 'USER_AUTH_ON' )) {
			import ( '@.ORG.Right' );
			if (! Right::RightPass ()) {
				$this->assign("jumpUrl",__APP__.C("USER_AUTH_GATEWAY"));
				$this->error ( '你无权操作！' );
			}
		}
	}
	//首页
	public function index() {
		if (isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {
			$this->display ();
		} else {
			$this->redirect ( 'Right/login' );
		}
	
	}
	//登录操作
	public function login() {
		$this->display ();
	}
	//设置权限的操作
	public function set() {
		if ($this->isPost ()) {
			$role = M ( 'Role' );
			$map = array ();
			$map ['id'] = $_POST ['id'];
			$value = array_sum ( $_POST ['name'] ); //将取得的数值进行或运算相当于求和操作，但是原理是不相同的哦
			$res = $role->where ( $map )->setField ( 'shell', $value );
			if ($res !== false) {
				$this->success ( '保存成功！' );
			} else {
				$this->error ( '保存失败' );
			}
		} else {
			$id = $_GET ['id'];
			$this->assign ( 'id', $id );
			$this->display ( 'Right:index' );
		}
	}
	
	//各项操作方法
	public function add() {
		$this->display ();
	}
	public function del() {
		$this->display ();
	}
	public function select() {
		$this->display ();
	}
	public function edite() {
		$this->display ();
	}
	
	//验证登录操作
	public function checklogin() {
		if (empty ( $_POST ['username'] )) {
			$this->error ( '帐号错误！' );
		} elseif (empty ( $_POST ['passwd'] )) {
			$this->error ( '密码必须！' );
		}
		$map ['username'] = $_POST ['username'];
		import ( '@.ORG.Right' );
		$authInfo = Right::authCheck ( $map );
		if (false === $authInfo) {
			$this->error ( '帐号不存在或已禁用！' );
		} else {
			if ($authInfo ['passwd'] != md5 ( $_POST ['passwd'] )) {
				$this->error ( '密码错误！' );
			}
			$_SESSION [C ( 'USER_AUTH_KEY' )] = $authInfo ['id'];
			//如果为管理员
			if ($authInfo ['account'] == 'admin') {
				$_SESSION [C ( 'ADMIN_AUTH_KEY' )] = true;
			}
			Right::saveRight ();
			$this->redirect ( 'Right/index', '', 3, '登录成功！页面跳转中~~' );
		}
	}
	public function logout() {
		if (isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {
			UNSET ( $_SESSION [C ( 'USER_AUTH_KEY' )] );
			session_destroy ();
			$this->redirect ( 'Right/login' );
		}
	}
}
?>